<template>
  <div>
    <!-- <TableSearch :query="query" :options="searchOpt" :search="handleSearch" /> -->
    <div class="container">
      <TableCustom
        :columns="columns"
        :tableData="tableData"
        :total="page.total"
        :currentPage="page.currPage"
        :viewFunc="handleView"
        :delFunc="handleDelete"
        :change-page="changePage"
        :editFunc="handleEdit"
      >
        <!-- <template #toolbarBtn>
            <el-button type="warning" :icon="CirclePlusFilled" @click="handleAdd"
              >新增</el-button
            >
          </template> -->
      </TableCustom>
    </div>
    <el-dialog
      :title="isEdit ? '编辑' : '新增'"
      v-model="visible"
      width="700px"
      destroy-on-close
      :close-on-click-modal="closeDialog"
      @close="closeDialog"
    >
      <TableEdit
        :form-data="rowData"
        :options="options"
        :edit="isEdit"
        :update="updateData"
        @closeDialogP="closeDialog"
      />
    </el-dialog>
    <el-dialog title="详情" v-model="visible1" width="700px" destroy-on-close>
      <TableDetail
        :data="viewData"
        @closeDialogP="visible1 = false"
      ></TableDetail>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="system-user">
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { CirclePlusFilled } from "@element-plus/icons-vue";
import { User } from "@/types/user";
import {
  fetchUserDownloadList,
  fetchUsertCreate,
  fetchDeleteUser,
} from "@/api/userManage";
import TableCustom from "@/components/table-custom.vue";
import TableDetail from "@/components/table-detail.vue";
import TableSearch from "@/components/table-search.vue";
import { FormOption, FormOptionList } from "@/types/form-option";

const props = defineProps({
  operateAccountIdSelect1: {
    type: String,
    required: true,
  },
  operateAccountNameSelect1: {
    type: String,
    required: true,
  },
});

// 查询相关
const query = reactive({
  keyword: "",
});
const searchOpt = ref<FormOptionList[]>([]);
const handleSearch = () => {
  changePage(1);
};

// 表格相关
let columns = ref([
  { type: "index", label: "序号", width: 55, align: "center" },
  { prop: "songName", label: "歌曲名" },
  { prop: "performer", label: "表演者" },
  { prop: "createTime", label: "下载时间" },
  // { prop: "operator", label: "操作", width: 250 },
]);
const page = reactive({
  currPage: 1,
  size: 10,
  total: 0,
});
const tableData = ref([]);
const getData = async () => {
  let params = {
    currPage: page.currPage,
    size: page.size,
    keyword: query.keyword,
    operateAccountId: props.operateAccountIdSelect1,
    operateAccountName: props.operateAccountNameSelect1,
  };
  const res = await fetchUserDownloadList(params);
  console.log("用户下载列表管理");
  console.log(res.data);
  let list = res.data.data.records;
  list.forEach((item) => {
    tableData.value.push(item.songInfo);
  });
  console.log(tableData.value);
  page.total = res.data.data.total;
  console.log(page.total);
};
getData();

// 分页
const changePage = (val: number) => {
  console.log("点击分页---歌单分类管理");

  page.currPage = val;
  console.log(page.currPage);
  getData();
};

// 新增/编辑弹窗相关
let options = ref<FormOption>({
  labelWidth: "100px",
  span: 24,
  list: [
    { type: "input", label: "分类名称", prop: "categoryName", required: true },
    { type: "input", label: "分类描述", prop: "categoryDesc", required: true },
  ],
});
const visible = ref(false);
const isEdit = ref(false);
const rowData = ref({});

// 新增
const handleAdd = (row) => {
  console.log("新增");
  rowData.value = { ...row };
  isEdit.value = false;
  visible.value = true;
};

// 编辑
const handleEdit = (row) => {
  console.log("编辑");
  rowData.value = { ...row };
  isEdit.value = true;
  visible.value = true;
};
const updateData = async (data) => {
  let datas = {};
  if (isEdit.value) {
    datas = {
      categoryName: data.categoryName,
      categoryDesc: data.categoryDesc,
      playlistCategoryId: data.playlistCategoryId,
    };
  } else {
    datas = {
      categoryName: data.categoryName,
      categoryDesc: data.categoryDesc,
    };
  }
  console.log("最终编辑或者新增的数据---是");
  console.log(datas);
  const res = await fetchUsertCreate(datas);
  console.log("新增歌单分类");
  console.log(res.data);
  closeDialog();
  getData();
};

const closeDialog = () => {
  visible.value = false;
  isEdit.value = false;
};

// 详情弹窗相关
const visible1 = ref(false);
const viewData = ref({
  row: {},
  list: [],
});
const handleView = (row) => {
  viewData.value.row = { ...row };
  viewData.value.list = [
    {
      prop: "playlistCategoryId",
      label: "分类ID",
    },
    {
      prop: "categoryName",
      label: "分类名称",
    },
    {
      prop: "categoryDesc",
      label: "分类描述",
    },
    {
      prop: "playlistCount",
      label: "歌单数量",
    },
    {
      prop: "createTime",
      label: "注册时间",
    },
  ];
  visible1.value = true;
};

// 删除相关
const handleDelete = async (row) => {
  console.log("删除");
  console.log(row);
  let datas = {
    id: row.playlistCategoryId,
  };
  const res = await fetchDeleteUser(datas);
  if (res.data.code == 0) {
    ElMessage.success("删除成功");
    getData();
  }
};
</script>

<style scoped></style>
